package cn.framelife.rainbow.db.creator.part;

import java.io.File;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;

import cn.framelife.rainbow.constant.Constant;
import cn.framelife.rainbow.db.entity.Menu;
import cn.framelife.rainbow.db.entity.MenuList;
import cn.framelife.rainbow.util.FileUtil;
import cn.framelife.rainbow.util.FreemarkerUtil;
import cn.framelife.rainbow.util.StringUtil;

/**
 * 用于生成与更新MenuData.json文件
 * 
 * @author 潘广伟 p_3er@qq.com  
 * @date 2014-1-15 上午9:52:20
 */
public class MenuCreator {
	private String projectName;
	private String packageURL;
	
	public MenuCreator(String projectName, String packageURL) {
		this.projectName = projectName;
		this.packageURL = packageURL;
	}
	
	public void createrCodeForMenu(MenuList properties) throws Exception{
		String dataPath = FileUtil.getMenuDataPath(projectName);
		if(new  File(dataPath).exists()){
			updateMenuData(dataPath, properties);
		}else{
			createMenuData(dataPath, properties);
			FreemarkerUtil.createMenuAction(projectName,StringUtil.merge(packageURL,".system.struts"));
		}
	}
	/**
	 * 新增
	 *
	 * @param dataPath
	 * @param menus
	 * @throws Exception 
	 */
	private void createMenuData(String dataPath,MenuList menus) throws Exception{
		if(menus.getParents().size() == 0){
			Menu menu = new Menu();
			menu.setId("menuA");
			menu.setText("菜单A");
			menus.add(menu);
		}
		String json = list2Json(menus);
		FileUtils.write(new File(dataPath), json, Constant.ECODING);
	}
	
	/**
	 * 更新文件
	 *
	 * @param dataPath
	 * @param newMenus
	 * @throws Exception
	 */
	private  void updateMenuData(String dataPath,MenuList newMenus) throws Exception{
		String data = FileUtils.readFileToString(new File(dataPath), Constant.ECODING);
		MenuList oldDataMenus = getOldDataMenus(data);
		
		for(Menu menu : newMenus){
			oldDataMenus.append(menu);
		}
		
		createMenuData(dataPath, oldDataMenus);
	}
	
	private  String list2Json(MenuList list){
		StringBuffer buffer = new StringBuffer("{children:[");
		
		for(Menu parent : list.getParents()){
			buffer.append("{id:'").append(parent.getId()).append("',text:'")
				.append(parent.getText()).append("',children:[");
			
			for(Menu children : list.getChildren(parent)){
				buffer.append("{id:'").append(parent.getId()).append(".").append(children.getId()).append("',text:'")
				.append(children.getText()).append("',leaf:true},");
			}
			
			buffer.append("]},");
		}
		
		buffer.append("]}");
		return buffer.toString();
	}
	
	private  MenuList getOldDataMenus(String data){
		JSONObject json=JSONObject.fromObject(data);
		JSONArray parents =  json.getJSONArray("children");
		MenuList list = new MenuList();
		Menu menu;
		for(int i = 0; i < parents.size(); i++){
			JSONObject parant = parents.getJSONObject(i);
			menu = new Menu();
			menu.setId(parant.getString("id"));
			menu.setText(parant.getString("text"));
			menu.setLeaf(false);
			list.add(menu);
			JSONArray rows = parant.getJSONArray("children");
			
			for(int j = 0; j < rows.size(); j++){
				JSONObject row = rows.getJSONObject(j);
				menu = new Menu();
				menu.setId(row.getString("id"));
				menu.setText(row.getString("text"));
				menu.setLeaf(row.getBoolean("leaf"));
				menu.setParentId(parant.getString("id"));
				menu.setParentText(parant.getString("text"));
				list.add(menu);
			}
		}
		
		return list;
	}
	
}
